using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.DirectoryServices.ActiveDirectory;
using System.DirectoryServices;

namespace SCHD
{
    public partial class WFResetPass : TDV.Controles.Base.WinForm.BaseForm_Tipo01
    {

        private string Login = "";
        public WFResetPass()
        {
            InitializeComponent();
        }
        public WFResetPass( string login)
        {
            this.Login = login;
            InitializeComponent();
        }

        private void BtnAceptar_Click(object sender, EventArgs e)
        {
            try
            {
            ResetPassword(TxtUsuario.Text, TxtClave.Text);
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
        }
        public static void ResetPassword(string userCN, string newpassword)
        {
            string dn = "TDV.COM";
            string admin = "TDV\\administrator";
            string adminpass = @"20104498044/$*";
            DirectoryContext objContext = new DirectoryContext(DirectoryContextType.Domain, dn, admin, adminpass);
            Domain objDomain = Domain.GetDomain(objContext);
            DirectoryEntry de = objDomain.GetDirectoryEntry();
            //geting user password    
            DirectoryEntry passUser = de.Children.Find("OU=TDV.COM").Children.Find("CN=" + userCN);
            using (passUser)
            {
                //this is the common error line if you do not set correct configuration        
                passUser.Invoke("SetPassword", new object[] { newpassword });
                passUser.CommitChanges();//don't forget to commint  
            }
        }

        private void BtnCancelar_Click(object sender, EventArgs e)
        {
            DialogResult = DialogResult.Cancel;
        }

        private void WFResetPass_Load(object sender, EventArgs e)
        {
            TxtUsuario.Enabled = false;
            TxtUsuario.Text = Login;
        }
    }
}

